#include "common/ad_logger.h"

#include <log4cxx/propertyconfigurator.h>

#include <filesystem>
namespace autodrive {
namespace perception {

log4cxx::LoggerPtr ADLogger::logger_;

ADLogger::ADLogger() {}

ADLogger::~ADLogger() {}

ADLogger &ADLogger::GetInstance() noexcept {
  static ADLogger instance;
  return instance;
}

void ADLogger::Init(std::string const &loggerName) noexcept(false) {
  auto initFunc{[&]() {
    auto const &propertyFile{"config/log4cxx.properties"};
    if (!std::filesystem::exists(propertyFile)) {
      throw std::runtime_error("file " + std::string(propertyFile) +
                               " does not exist");
    }
    log4cxx::PropertyConfigurator::configureAndWatch(propertyFile);
    logger_ = log4cxx::Logger::getLogger(loggerName);
    if (logger_ == nullptr) {
      throw std::runtime_error("get logger " + loggerName + " fail");
    }
  }};

  std::call_once(init_flag_, initFunc);
  is_init_ = true;
}
}  // namespace perception
}  // namespace autodrive